package com.tang.thepath.service;

import com.tang.thepath.model.BaseResponse;
import com.tang.thepath.model.RequestModel;

import java.util.List;

import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;
import rx.Observable;
import rx.functions.Func1;

public class BizService extends BaseService{

    private interface API {

        /**
         * 位置上报
         * @param request
         * @return
         */
        @POST("/api/frontend/reportposition")
        Observable<BaseResponse<RequestModel.ReportPositionResponse>> reportPosition(@Body RequestModel.ReportPositionRequest request);


        /**
         * 客户信息上报
         * @param request
         * @return
         */
        @POST("/api/frontend/storecustom")
        Observable<BaseResponse<RequestModel.StoreCustomResponse>> storeCustom(@Body RequestModel.StoreCustomRequest request);

        /**
         * 客户信息列表
         * @param page
         * @return
         */
        @GET("/api/custom")
        Observable<BaseResponse<RequestModel.ListCustomResponse>> listCustom(@Query("page") int page);

        /**
         * 上报销售信息
         * @param request
         * @return
         */
        @POST("/api/frontend/storesoldinfo")
        Observable<BaseResponse<RequestModel.StoreSoldInfoResponse>> storeSoldInfo(@Body RequestModel.StoreSoldInfoRequest request);


        /**
         * 上报拜访记录
         * @param request
         * @return
         */
        @POST("/api/frontend/storevisitrecord")
        Observable<BaseResponse<RequestModel.StoreVisitResponse>> storeVisit(@Body RequestModel.StoreVisitRequest request);


        /**
         * 上报售后服务记录
         * @param request
         * @return
         */
        @POST("/api/frontend/storerepair")
        Observable<BaseResponse<RequestModel.SaveRepairResponse>> saveRepair(@Body RequestModel.SaveRepairRequest request);

        /**
         * 上报售后问题
         * @param request
         * @return
         */
        @POST("/api/frontend/storeproblem")
        Observable<BaseResponse<RequestModel.IdResponse>> saveProblem(@Body RequestModel.SaveProblemRequest request);


        /**
         * 拜访记录列表
         * @param page
         * @return
         */
        @GET("/api/beinvited")
        Observable<BaseResponse<RequestModel.ListVisitResponse>> listVisit(@Query("page") int page);

        /**
         * 售后服务列表
         * @param page
         * @return
         */
        @GET("/api/repair")
        Observable<BaseResponse<RequestModel.ListRepairResponse>> listRepair(@Query("page") int page);

        /**
         * 售后服务列表
         * @param page
         * @return
         */
        @GET("/api/problem")
        Observable<BaseResponse<RequestModel.ListProblemResponse>> listProblem(@Query("page") int page);

        /**
         * 用户列表
         * @param level
         * @param role
         * @return
         */
        @GET("/api/manager")
        Observable<BaseResponse<RequestModel.ListUserResponse>> listUser(@Query("level") int level,@Query("role")int role);

        /**
         * 获得所有用户列表
         * @return
         */
        @GET("/api/getallmember")
        Observable<BaseResponse<RequestModel.ListUserResponse>> getAllMember();
        /**
         * 更新维修后照片
         * @param request
         * @return
         */
        @POST("/api/frontend/updaterepair")
        Observable<BaseResponse<RequestModel.IdResponse>> updateRepair(@Body RequestModel.UpdateRepairRequest request);
    }



    public static Observable<RequestModel.ReportPositionResponse> reportPosition(RequestModel.ReportPositionRequest request){
        return toSubscribe(HttpManager.getService(API.class).reportPosition(request));
    }

    public static Observable<RequestModel.StoreCustomResponse> storeCustom(RequestModel.StoreCustomRequest request){
        return toSubscribe(HttpManager.getService(API.class).storeCustom(request));
    }

    public static Observable<RequestModel.StoreSoldInfoResponse> storeSoldInfo(RequestModel.StoreSoldInfoRequest request){
        return toSubscribe(HttpManager.getService(API.class).storeSoldInfo(request));
    }

    public static Observable<RequestModel.StoreVisitResponse> storeVisit(RequestModel.StoreVisitRequest request){
        return toSubscribe(HttpManager.getService(API.class).storeVisit(request));
    }

    public static Observable<RequestModel.ListCustomResponse> listCustom(int page){
        return toSubscribe(HttpManager.getService(API.class).listCustom(page)).map(new Func1<RequestModel.ListCustomResponse, RequestModel.ListCustomResponse>() {
            @Override
            public RequestModel.ListCustomResponse call(RequestModel.ListCustomResponse listCustomResponse) {
                List<RequestModel.CustomInfo> customInfoList = listCustomResponse.customInfoList;
                for (int i=0;i<customInfoList.size();i++){
                    List<String> images = customInfoList.get(i).images;
                    for (int j=0;j<images.size();j++){
                        images.set(j,HttpManager.BASEURL+images.get(j));
                    }
                }
                return listCustomResponse;
            }
        });
    }

    public static Observable<RequestModel.SaveRepairResponse> saveRepair(RequestModel.SaveRepairRequest request){
        return toSubscribe(HttpManager.getService(API.class).saveRepair(request));
    }

    public static Observable<RequestModel.IdResponse> saveProblem(RequestModel.SaveProblemRequest request){
        return toSubscribe(HttpManager.getService(API.class).saveProblem(request));
    }

    public static Observable<RequestModel.ListVisitResponse> listVisit(int page){
        return toSubscribe(HttpManager.getService(API.class).listVisit(page));
    }

    public static Observable<RequestModel.ListRepairResponse> listRepair(int page){
        return toSubscribe(HttpManager.getService(API.class).listRepair(page)).map(new Func1<RequestModel.ListRepairResponse, RequestModel.ListRepairResponse>() {
            @Override
            public RequestModel.ListRepairResponse call(RequestModel.ListRepairResponse listRepairResponse) {
                List<RequestModel.Repair> repairList = listRepairResponse.repairList;
                for (int i=0;i<repairList.size();i++){
                    List<String> images = repairList.get(i).images;
                    List<String> afterImages = repairList.get(i).afterImages;
                    for (int j=0;j<images.size();j++){
                        images.set(j,HttpManager.BASEURL+images.get(j));
                    }
                    for (int j=0;j<afterImages.size();j++){
                        afterImages.set(j,HttpManager.BASEURL+afterImages.get(j));
                    }
                }
                return listRepairResponse;
            }
        });
    }

    public static Observable<RequestModel.ListProblemResponse> listProblem(int page){
        return toSubscribe(HttpManager.getService(API.class).listProblem(page));
    }

    public static Observable<RequestModel.ListUserResponse> listUser(int level,int role){
        return toSubscribe(HttpManager.getService(API.class).listUser(level,role));
    }

    public static Observable<RequestModel.ListUserResponse> getAllMember(){
        return toSubscribe(HttpManager.getService(API.class).getAllMember());
    }

    public static Observable<RequestModel.IdResponse> updateRepair(RequestModel.UpdateRepairRequest request){
        return toSubscribe(HttpManager.getService(API.class).updateRepair(request));
    }
}
